package acm;

import java.io.PrintStream;
import java.util.Scanner;

public class Move
{
	PrintStream out = System.out;
	static Scanner in = new Scanner(System.in);
	
	int row,colum;
	int cell[][];
		
	int maxDistance = Integer.MIN_VALUE;
	
	public Move()
	{
		row = in.nextInt();
		colum = in.nextInt();
		
		cell = new int[row][colum];
		
		for(int y=0; y<row; y++)
		{
			for(int x=0; x<colum; x++)
			{
				int input = in.nextInt();
				
				cell[y][x] = input;
			}
		}
		
		for(int y=0; y<row; y++)
			for(int x=0; x<row; x++)
				move(x, y, 1);
		
		out.println(maxDistance);
	}
	
	public void move(int x, int y, int distance)
	{
		if(x>0 && cell[y][x-1] < cell[y][x])
		{
			move(x-1, y, distance + 1);
		}

		if(x<colum-1 && cell[y][x+1] < cell[y][x])
		{
			move(x+1, y, distance + 1);
		}

		if(y>0 && cell[y-1][x] < cell[y][x])
		{
			move(x, y-1, distance + 1);
		}

		if(y<row-1 && cell[y+1][x] < cell[y][x])
		{
			move(x, y+1, distance + 1);
		}

		if(distance > maxDistance)
		{
			maxDistance = distance;
		}
	}
	
	public static void main(String[] args)
	{
		for(int i=in.nextInt(); i>0; i--)
			new Move();
	}
}
